#include<iostream>
using std::endl;
using std::cout;
#include<map>
using std::map;
#include<vector>
using std::vector;
#include<queue>
using std::priority_queue;
#include <unordered_map>
using std::unordered_map;
using std::make_pair;

typedef struct v4tims_s{
    int key;    //key //存放的是key
    int value;  //value;
    int times;  //次数
}v4tims_t;
class Compare{
    public:
    bool operator()(const  v4tims_t&rhs,const v4tims_t &lhs){
        if(rhs.times > lhs.times){
            return true;
        }
        else{
            return false;
        }
    }
};

typedef unordered_map<int,int> _map; //第一个value，第二个是 times
class LRUCache {
public:
    LRUCache(int capacity);
    void get(int key);
    void put(int key, int value);
    friend
    bool &operator >(const LRUCache&rhs, const LRUCache&lhs);
    void getPriority(); //查看是否是最小的

    //建立_pri_que
    void bulid_PRI(v4tims_t value);
    void check_pri(){
        cout<<"key: "<<_pri_que.top().key
        <<" value: "<<_pri_que.top().value
        <<"times: "<<_pri_que.top().times<<endl;
    }
private:
    int _capacity; //容器
    //map 存放key,value,times(次数) map<key,<value,times>> _map
      //std::pair 是一个复合类型，不支持哈希函数，默认情况下无法作为 unordered_map 的键。
    //小顶堆
    //unordered_map<vector<int,int>,int> _chche; error写法
    unordered_map<int,v4tims_t> _chche;  
    priority_queue<v4tims_t,vector<v4tims_t>,Compare> _pri_que;
};

LRUCache::LRUCache(int capacity)
:_capacity(capacity){}

void LRUCache::get(int key){
    auto it = _chche.find(key);
    if(it!=_chche.end()){
        //如果找到了 次数+1;
        cout<<"++ times"<<endl;
        it->second.times++;
        cout<<it->second.times<<endl;//要修改pri的值
        //找到que的值
        priority_queue<v4tims_t,vector<v4tims_t>,Compare> tempQue;
        while(!_pri_que.empty()){
            v4tims_s temp = _pri_que.top();
            _pri_que.pop();
            if(temp.key != _pri_que.top().key){
                temp.times++;
            }
            tempQue.push(temp);
        }
        _pri_que = tempQue;
    }else{
        //没有找到的情况
       cout<<"null"<<endl;
    }
}
//put 操作
void LRUCache::put(int key, int value){
    auto it2 = _chche.find(key);
    //找到了
    if(it2 != _chche.end()){
        //给新值
        cout<<"find value"<<endl;
        it2->second.value = value;
    }else{
        cout<<"find no but insert"<<endl;
        v4tims_t temp;
        //检查容量
        //此时容量满了
        if(_chche.size() == _capacity){
            //删除顶部元素    
            getPriority();
        }
        //插入新值
        temp.key = key;
        temp.times = 1;
        temp.value = value;
        _chche.insert({key,temp});//error
        bulid_PRI(temp);
    }
}
void LRUCache::bulid_PRI(v4tims_t value){
    _pri_que.push(value);
}
void LRUCache::getPriority(){
    int key = _pri_que.top().key;
    _pri_que.pop();
    _chche.erase(key); //移除key
}

int test(){
    LRUCache LRUCache(10);
    LRUCache.put(1,2);
    LRUCache.get(1);
    LRUCache.get(1);
    LRUCache.check_pri(); 
}
int main(void){
    test();
}